home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12202 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: newsroom.hitc.com!usenet
  2. From: Ben Jones <bjones@eos.hitc.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: ?Callbacks / Pointer to member functions?
  5. Date: Mon, 18 Mar 1996 09:21:43 -0500
  6. Organization: Hughes Team (EOSDIS)
  7. Message-ID: <314D7177.6549@eos.hitc.com>
  8. References: <4i9b3s$7tt@serveru1.naic.wpafb.af.mil>
  9. NNTP-Posting-Host: lo-mac2134a.hitc.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Macintosh; I; 68K)
  14.  
  15. jimmie j rohrer/scde/77815 wrote:
  16. >         1) How can I get a SINGLE pointer to a member function that
  17. > can be invoked when it is passed to some third party?
  18. >         -or-
  19. >         2) Is there some clever way to get motif to call any given
  20. > member function from a 'function' callback?
  21.  
  22. There is a way to do it.  Unfortunately it is compiler and platform 
  23. dependent and the technical details are not typically published.  A
  24. pointer to member function consists of 3 pieces of information encoded
  25. into 8 bytes (16 on a Dec Alpha):
  26.  
  27.     A flag which indicates if a virtual function is involved.
  28.  
  29.     The address of the actual function, if it is non virtual.  The
  30.     offset into the vtable, if it is virtual.
  31.  
  32.     The offset into the object of the address of the vtable.
  33.  
  34. If you can find out which fields are which, then it is possible to
  35. obtain the address of the actual function to be called.  This
  36. function will have the same prototype as the member function
  37. except that it has an additional argument at the beginning of
  38. the argument list which is the pointer to the object it is to
  39. operate upon.  That is, a member function with the prototype:
  40.  
  41.     type (classname::*)(type1,type2,...)
  42.  
  43. actually has the prototype:
  44.  
  45.     type (*)(classname*,type1,type2,...)
  46.  
  47. Which brings to mind the question:  Why can't the C++ language
  48. deal with the expressions:
  49.  
  50.     object.function_name
  51.     pointer->function_name
  52.  
  53. unless parentheses are applied.  It can deal with
  54.  
  55.     function_name
  56.  
  57. just fine.  All it would take would be a way to declare the
  58. container to hold such an expression.  Just as we can say:
  59.  
  60.     type (*pf)(type1,type2,...) = function_name;  // Store now
  61.         ...
  62.     pf(arg1,arg2,...)                             // execute later
  63.  
  64. a simple extension to C++ would make it possible to say:
  65.  
  66.     type (::*pbmf)(type1,type2,...) = object.function_name;
  67.         ...
  68.     pbmf(arg1,arg2,...)
  69.  
  70. The <pbmf> (pointer to bound member function) would contain the object
  71. pointer and the address of the actual function to be applied to that
  72. object.  The compiler could handle overload resolution in this case
  73. just as easily as it does for the non-member-function case.  It could
  74. generate the appropriate code for obtaining the function address just 
  75. as easily as it does for generating the internal fields of a
  76. pointer-to-member-function.
  77.  
  78. COMPILER WRITERS, DO US ALL A BIG FAVOR AND IMPLEMENT THIS, PLEASE.
  79.  
  80. We could then write much simpler class libraries.  Instead of having
  81. to sub-class in order to override dummy functions, we could simply
  82. do the intuitively obvious:
  83.  
  84.     a.out = b.in
  85.  
  86. so that when "out(...)" is called in this instance of class A,
  87. "b.in(...)" is called.
  88. -- 
  89. Ben Jones
  90. Hughes Information Technology
  91. bjones@eos.hitc.com
  92.